﻿package common.components.transition.transitions{
	import common.BaseCons;
	import common.components.transition.events.TransitionAutoEvent;
	import common.components.transition.events.TransitionEvent;
	import common.components.transition.events.TransitionLoadEvent;
	import common.components.transition.events.TransitionPlayEvent;
	import common.components.transition.transitions.mask.Clock;
	import common.components.transition.transitions.mask.HWindow;
	import common.components.transition.transitions.mask.Heart;
	import common.components.transition.transitions.mask.Point;
	import common.components.transition.transitions.mask.Round;
	import common.components.transition.transitions.mask.Round2;
	import common.components.transition.transitions.mask.Star;
	import common.components.transition.transitions.mask.Updown;
	import common.components.transition.transitions.mask.VWindow;
	import common.components.transition.transitions.mosaic.Window;
	import common.components.transition.transitions.tween.Fade;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.Application;
			public class TransitionRunner extends EventDispatcher	{			private var imageItems : Array;		private var arrStayTime:Array;				[Bindable]		public var idx : int;				private var loadIdx : int;				private var stage:Canvas;				public var _auto:Boolean;				private var goto:int;		[Bindable]		public var fullyLoaded:Boolean;				private var transTime:Number;		private var stayTime:Number;		private var loadPages:int;				[Bindable]		public var paused:Boolean;		private var trans:Transition;				private var lastTrans:Transition;//-----arming add				public function TransitionRunner(imageItems:Array, arrStayTime:Array, stage:Canvas,transTime:Number=1.5,stayTime:Number=2,loadPages:int=0) 		{			this.stage = stage;			this.imageItems = imageItems;			this.arrStayTime = arrStayTime;			this.transTime = transTime;			this.stayTime = stayTime;			this.loadPages = loadPages;						this.reset();		}				public function setImageItems( imgItems:Array ):void		{			this.imageItems = imgItems;		}				public function setStayTimes( times:Array ):void		{			this.arrStayTime = times;		}				public function getLength():int		{			return this.imageItems.length;		}				public function reset():void		{			this.trans = null;			this.idx = -1;			this.loadIdx = 0;			this.auto = false;			this.paused = false;			this.fullyLoaded = false;			this.goto = -1;						for ( var i:int = 0; i < this.imageItems.length; ++i )			{				var imageItem:ImageItem = this.imageItems[i];				imageItem.loaded = false;//				imageItem.toPlay = false;				imageItem.image = null;			}						this.load();		}				public function continueLoad(loadPages:int=0):void		{			this.loadPages = loadPages;			this.load();		}				public function free():void		{			for each( var imgItem:ImageItem in this.imageItems )			{				if( imgItem.image )				{					imgItem.image.source = '';					imgItem.image.load( null );					imgItem.image = null;				}			}		}				private function load():void		{			if (loadIdx == -1)				return;							this.dispatchEvent(new TransitionLoadEvent(TransitionLoadEvent.EVENT_TRANS_LOAD,this.loadIdx,this.imageItems.length));						if ( loadIdx >= this.imageItems.length )			{				loadIdx = -1;				this.fullyLoaded = true;				return;				}			else if (this.loadPages !=0 && loadIdx >= this.loadPages)			{				return;			}						var imageItem:ImageItem = this.imageItems[loadIdx];					imageItem.image = new Image();			imageItem.image.addEventListener(Event.COMPLETE,this.onLoadComplete);		            imageItem.image.addEventListener(IOErrorEvent.IO_ERROR, this.onLoadFailed);			imageItem.image.scaleContent = true;			imageItem.image.maintainAspectRatio = false;			imageItem.image.width = stage.width;			imageItem.image.height = stage.height;			imageItem.image.source = imageItem.source;			if ( imageItem.source is String )			{				imageItem.loaded = false;				imageItem.image.autoLoad = false;				imageItem.image.load();			}			else if ( imageItem.source is Bitmap )				this.onLoadComplete();		}        private function onLoadFailed(evt:Event):void{        }				private function onLoadComplete(evt:Event=null):void		{			var imageItem:ImageItem = this.imageItems[loadIdx];					imageItem.image.removeEventListener(Event.COMPLETE,this.onLoadComplete);		            imageItem.image.removeEventListener(IOErrorEvent.IO_ERROR, this.onLoadFailed);			imageItem.loaded = true;						this.loadIdx++;			this.load();						if ( this.idx == -1 ) // first time			{				this.idx = 0;				this.play();				}//			else if ( this.imageItems[idx].toPlay )//			{//				this.play();//			}		}					[Bindable]		public function set auto(auto:Boolean):void		{			if ( this._auto == auto )				return;						// otherwise it's a switch						this._auto = auto;							this.dispatchEvent(new TransitionAutoEvent(TransitionAutoEvent.EVENT_TRANS_AUTO,this.auto));			//			// switch to auto			//			if ( this.auto && this.paused )//			{//				this.go(true,true);//				return;//			}						// switch to manual			if ( !this.auto && this.paused )			{				this.trans.cutStay();				return;			}					}				public function get auto():Boolean		{			return this._auto;		}		private function play() : void 		{			var imageItem:ImageItem = this.imageItems[idx];			stayTime = this.arrStayTime[idx];//			imageItems.toPlay = false;						imageItem.image.visible = false;//			this.stage.addChild(imageItem.image);//			this.stage.addChildAt(			this.stage.addChildAt(imageItem.image,0);						if(Application.application.url.indexOf('backadmin') > 0)			{				imageItem.type = -1;			}						switch ( imageItem.type ) 			{				case -1:					this.trans = new NoneFx(this,imageItem.image,this.transTime,this.stayTime);				break;								case BaseCons.TRANS_TYPE_FADE:					this.trans = new Fade(this,imageItem.image,this.transTime,this.stayTime);				break;								case BaseCons.TRANS_TYPE_HEART:					this.trans = new Heart(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_CLOCK:					this.trans = new Clock(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_WINDOW:					this.trans = new Window(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_POINT:					this.trans = new Point(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_ROUND:					this.trans = new Round(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_STAR:					this.trans = new Star(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_UPDOWN:					this.trans = new Updown(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_HWINDOW:					this.trans = new HWindow(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_VWINDOW:					this.trans = new VWindow(this,imageItem.image,this.transTime,this.stayTime);				break;				case BaseCons.TRANS_TYPE_ROUND2:					this.trans = new Round2(this,imageItem.image,this.transTime,this.stayTime);				break;			}						this.trans.addEventListener(TransitionEvent.TRANS_OUT_END,onTransOutEnd);			this.trans.addEventListener(TransitionEvent.TRANS_OUT_START,onTransOutStart);			this.trans.addEventListener(TransitionEvent.TRANS_STAY_START,onTransStayStart);			this.trans.addEventListener(TransitionEvent.TRANS_STAY_END,onTransStayEnd);							this.trans.walkIn();						if ( this.idx == this.imageItems.length-1 )			{				//last page				this.dispatchEvent(new TransitionPlayEvent(TransitionPlayEvent.EVENT_TRANS_PLAY,idx,true,true));				}			else			{				this.dispatchEvent(new TransitionPlayEvent(TransitionPlayEvent.EVENT_TRANS_PLAY,idx,true));				}					}				public function onTransStayStart(evt:Event=null):void		{			this.dispatchEvent(new TransitionPlayEvent(TransitionPlayEvent.EVENT_TRANS_PLAY,idx,false));			Transition(evt.target).removeEventListener(TransitionEvent.TRANS_STAY_START,onTransStayStart);			this.paused = true;			//			// now to can call this.trans.walkOut() when receive the user action.		}				public function goTo(idx:int,auto:Boolean=false):void		{						if (  this.trans && this.idx != idx && idx >= 0 && idx < this.imageItems.length )			{										if ( this.fullyLoaded || idx < this.loadIdx )				{					this.auto = auto;					this.goto = idx;					if(lastTrans) lastTrans.cutWalkOut();//-------arming aDD					lastTrans = this.trans;//-------arming aDD					this.trans.walkOut();					trace('goTo'+idx);				}								// otherwise pause here.			}		}		public function goToEnd():void		{			if ( this.idx != this.imageItems.length-1 )			{										this.goTo(this.imageItems.length-1);			}		}				public function goToBeginning():void		{			if ( this.idx != 0 )			{										this.goTo(0);			}		}				public function go(forward:Boolean,auto:Boolean):void		{			var newIdx:int;			if ( forward )			{				newIdx = (this.idx+1)%this.imageItems.length;			}			else			{				newIdx = this.idx - 1;								if ( newIdx < 0 )				{					newIdx += this.imageItems.length;				}			}			trace("go");			this.goTo(newIdx,auto);		}				private function onTransStayEnd(evt:Event):void		{			Transition(evt.target).removeEventListener(TransitionEvent.TRANS_STAY_END,onTransStayEnd);			this.paused = false;		}				private function onTransOutStart(evt:Event):void		{			Transition(evt.target).removeEventListener(TransitionEvent.TRANS_OUT_START,onTransOutStart);						var newIdx:int;									if ( this.goto >= 0 && this.goto < this.imageItems.length ) // valid			{				newIdx = this.goto;				trace('newidx'+newIdx);			}			else			{				return; // break out.			}						if ( this.imageItems.length == 1 ) // on one image			{				this.idx = -1;				return;			}						this.idx = newIdx;						if ( this.imageItems[idx].loaded )				this.play();			else				trace('warning:try to play an unloaded image! should be blocked by goto');//			else//				this.imageItems[idx].toPlay = true;				//		else	//			wait animation		}		private function onTransOutEnd(evt:Event):void		{			var trans:Transition = Transition(evt.target);			this.stage.removeChild(trans.image);												Transition(evt.target).removeEventListener(TransitionEvent.TRANS_OUT_END,onTransOutEnd);		}		}}